<template>
  <div class="user">
    <div class="user_top">
      <img :src="avatar" alt="" width="70" />
      <h3 v-if="!ifLogined" @click="ifShowModal = true">点击登录</h3>
      <h3 v-else>{{ username }}</h3>
      <van-icon
        :name="ifLogined ? 'cross' : 'arrow'"
        @click="loginOut"
      ></van-icon>
    </div>
    <div>
      <van-grid square :column-num="3">
        <van-grid-item
          v-for="item in gridArr"
          :key="item.id"
          :icon="item.icon"
          :text="item.type"
        />
      </van-grid>
    </div>
    <div class="modal" v-show="ifShowModal">
      <div class="modal_bg" @click="ifShowModal = false"></div>
      <div class="modal_content">
        <van-form @submit="onSubmit">
          <van-field
            v-model="username"
            name="用户名"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="密码"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
          <div style="margin: 16px">
            <van-button round block type="primary" native-type="submit"
              >提交</van-button
            >
          </div>
        </van-form>
      </div>
    </div>
  </div>
</template>
 
<script>
import { LoginApi } from "../request/api";
export default {
  data() {
    return {
      ifShowModal: false,
      username: "",
      password: "",
      // 头像
      avatar: require("../assets/user.png"),
      gridArr: [
        { id: 0, icon: "label-o", type: "我的订单" },
        { id: 1, icon: "bill-o", type: "优惠券" },
        { id: 2, icon: "goods-collect-o", type: "礼品卡" },
        { id: 3, icon: "location-o", type: "我的收藏" },
        { id: 4, icon: "flag-o", type: "我的足迹" },
        { id: 5, icon: "contact", type: "会员福利" },
        { id: 6, icon: "aim", type: "地址管理" },
        { id: 7, icon: "warn-o", type: "账号安全" },
        { id: 8, icon: "service-o", type: "联系客服" },
        { id: 9, icon: "question-o", type: "帮助中心" },
        { id: 10, icon: "smile-comment-o", type: "意见反馈" },
      ],
      // 判断是否已经登录成功
      ifLogined: false,
    };
  },
  created() {
    // 判断localStorage中是否有token
    let token = localStorage.getItem("X-Nideshop-Token");
    if (token) {
      this.ifLogined = true;
      let userInfo = JSON.parse(localStorage.getItem("userInfo"));
      this.avatar = userInfo.avatar;
      this.username = userInfo.username;
    }
  },
  methods: {
    onSubmit(values) {
      console.log("submit", values);
      LoginApi({
        username: values["用户名"],
        pwd: values["密码"],
      }).then((res) => {
        console.log(res);
        if (res.errno == 0) {
          // 存储token
          localStorage.setItem("X-Nideshop-Token", res.data.token);
          // 关闭模态框
          this.ifShowModal = false;
          // 存储userInfo
          let str = JSON.stringify(res.data.userInfo);
          localStorage.setItem("userInfo", str);
          // 修改头像和用户名
          this.avatar = res.data.userInfo.avatar;
          this.username = res.data.userInfo.username;
          this.ifLogined = true;
        }
      });
    },
    // 登出
    loginOut() {
      if (this.ifLogined) {
        this.$dialog
          .confirm({
            title: "登出账号确认",
            message: "您是否想要退出账号",
          })
          .then(() => {
            // 清除token
            localStorage.removeItem("X-Nideshop-Token");
            // 刷新当前页面
            this.$router.go(0); 
          });
      } else {
        // 代表未登录，要打开模态框
        this.ifShowModal = true;
      }
    },
  },
};
</script>
 
<style lang = "less" scoped>
.user {
  .user_top {
    height: 70px;
    background: #333;
    color: #fff;
    padding: 20px 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    img {
      border-radius: 50%;
      margin-right: 10px;
    }
    h3 {
      flex: 1;
    }
  }
  .modal {
    z-index: 99;
    width: 100%;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    .modal_bg {
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.3);
    }
    .modal_content {
      padding-top: 30px;
      width: 90%;
      height: 200px;
      background-color: #fff;
      position: absolute;
      left: 0;
      bottom: 0;
      right: 0;
      top: 0;
      margin: auto;
    }
  }
}
</style>